﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataAccess;
using System.Data;

namespace Manager
{
    public partial class UserManager : System.Web.UI.Page
    {
        UserBusiness userBusiness = new UserBusiness();

        protected void Page_Load(object sender, EventArgs e)
        {if (!Page.IsPostBack)
            {
                Load_User();
            }
        }

        private void Load_User()
        {
            DataTable dtbUser = UserData.SelectAll();

            if (dtbUser.Rows.Count > 0)
            {
                rptUser.DataSource = dtbUser;
                rptUser.DataBind();
            }
            else
            {
                rptUser.Visible = false;
                noUser.Visible = true;
            }
        }

        protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int userID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "UserID"));
            Label lblType = (Label)e.Item.FindControl("lblType");
            Label lblStatus = (Label)e.Item.FindControl("lblStatus");
            Label lblCountNews = (Label)e.Item.FindControl("lblCountNews");

            UserInfo userInfo = UserData.SelectByUserID(userID);

            if (userInfo.UserStatus == true)
            {
                lblStatus.Text = "Hoạt động";
            } 
            else
            {
                lblStatus.Text = "Tạm khóa";
            }
        }

        protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int userID = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName.Equals("View"))
            {
                Response.Redirect("ViewUser.aspx?UserID=" + userID);
            }
            if (e.CommandName.Equals("Del"))
            {
                UserData.DeleteByUserID(userID);
                Response.Redirect("UserManager.aspx");
            }
            if (e.CommandName.Equals("ViewNews"))
            {
                Response.Redirect("NewsManager.aspx?UserID=" + userID);
            }
            if (e.CommandName.Equals("Lock"))
            {
                userBusiness.UpdateStatus(userID);
                Response.Redirect("UserManager.aspx");
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddUser.aspx");
        }
    }
}